<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/7/10
 * Time: 15:33
 */
namespace common\models;

use yii\db\ActiveRecord;

class HotSaleConfig extends ActiveRecord{
    const HOT_SALE_TYPE_1	= 1;	//热销商品组
    const HOT_SALE_TYPE_2	= 2;	//二级分类维护页
    const HOT_SALE_TYPE_3	= 3;	//首页banner活动页
    const HOT_SALE_TYPE_4	= 4;	//专题页
    const HOT_SALE_TYPE_5	= 5;	//广告位
    const HOT_SALE_TYPE_6	= 6;	//视频专区
    const HOT_SALE_TYPE_7	= 7;	//文章专区
    const HOT_SALE_TYPE_8	= 8;	//促销广告
    const HOT_SALE_TYPE_9	= 9;	//品牌图片
    const HOT_SALE_TYPE_11	= 11;	//五子棋

    const USER_FLG_YES	= 1;	//启用
    const USER_FLG_NO	= 0;	//禁用

	const PLATFORM_PC	= 1;	//pc专用
	const PLATFORM_WAP	= 2;	//WAP专用

	//add by FDG367 2018/03/14 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页 start
	const IS_SHOW		=1;		//类型（HOT_SALE_TYPE_9==9）首页是否展示  1：展示
	const NO_SHOW		=0;		//类型（HOT_SALE_TYPE_9==9）首页是否展示  0：不展示
	//add by FDG367 2018/03/14 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页 end

    public static function tableName(){
        return '{{%hot_sale_config}}';
    }

	/**
	 * @param $cateId 分类ID 获取对应分类下的图片FDG367 20171103
	 * @param int $type 图片类型
	 * @return array
	 */
	public static function getBannerInfo($cateId,$type=2,$platform=1){

		return BannerConfig::find()
			//->select(['group_id','id','group_name','picture_name','picture_url','picture_alt','product_url','sku','sort','ext1','ext2','ext3'])
			->joinWith('hotSaleConfig')
			->where([
				self::tableName().'.use_flg'	=>self::USER_FLG_YES,
				self::tableName().'.type'		=>$type,
				self::tableName().'.platform'	=>$platform,
				self::tableName().'.category_id'=>$cateId,
			])
			->orderBy([self::tableName().'.sort'=>SORT_DESC])
			->asArray()
			->all();
	}

    /**
     * @param $cateId 分类ID 获取对应分类下的图片FDG367 20171103
     * @param int $type 图片类型
     * @return array
     */
    public static function getHomeBannerInfo($type,$platform=2){

        return BannerConfig::find()->alias('b')
            ->select(['b.group_id','b.id','b.group_name','b.picture_name','b.picture_url','b.picture_alt','b.product_url','b.sku','b.sort','b.ext1','b.ext2','b.ext3',self::tableName().'.type'])
            ->joinWith('hotSaleConfig')
            ->where([
                self::tableName().'.use_flg'	=>self::USER_FLG_YES,
                self::tableName().'.type'		=>$type,
                self::tableName().'.platform'	=>$platform
            ])
            ->orderBy([self::tableName().'.sort'=>SORT_ASC])
            ->asArray()
            ->all();
    }

	/**
	 * @PARAM 获取对应配置信息 TODO 加上扩展信息
	 */
	public static function getConfigInfo($brandID,$type,$platform=2){
		return self::find()->where([
			'use_flg'	=>self::USER_FLG_YES,
			'type'		=>$type,
			'platform'	=>$platform,
			'brand_id'	=>$brandID,
		])->one();
	}

	/**
	 * @param add by FDG367 2018-03-08 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中） 关联表
	 * @return \yii\db\ActiveQuery
	 */
	public function getBannerConfig(){
		return $this->hasMany(BannerConfig::className(),['group_id'=>'id']);
	}

	/**
	 * @param add by FDG367 2018-03-08 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中） 关联表
	 * @return \yii\db\ActiveQuery
	 */
	public function getBrandCategory(){
		return $this->hasOne(GoodsBrand::className(),['id'=>'brand_id']);
	}

	/**
	 * @param add by FDG367 2018-03-08 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中） 获取对应品牌街的列表
	 * @param $brandStreetCategoryId
	 * @param $platform
	 * @param $page
	 * @param $page_num
	 */
	public static function getHotSaleConfigList(
		$brandStreetCategoryId,
		$type=HotSaleConfig::HOT_SALE_TYPE_9,
		$platform=HotSaleConfig::PLATFORM_PC,
		$page=1,
		$page_num=PC_NUM
	){
		$params	=array_filter([
			BannerConfig::tableName().'.brand_street_category_id'=>$brandStreetCategoryId,
			self::tableName().'.type'		=>$type,
			self::tableName().'.use_flg'	=>HotSaleConfig::USER_FLG_YES,
			self::tableName().'.platform'	=>$platform,
			//GoodsBrand::tableName().'.use_flg'	=>GoodsBrand::USE_FLG_YES,
		]);
		return  self::find()
			->joinWith('bannerConfig')
			//->joinWith('brandCategory')
			->where($params)
			->groupBy([
				BannerConfig::tableName().'.group_id'
			])
			->limit($page_num)
			->offset(($page-1)*$page_num)
			->orderBy([
				self::tableName().'.sort'=>SORT_ASC
			])
			//->createCommand()->getRawSql();
			->asArray()
			->all();

	}

	public static function getHotSaleAll($type,$platform=self::PLATFORM_WAP){
		return self::find()
			->where([
				'type' 		=>$type,
				'platform'	=>$platform,
				'use_flg'	=>self::USER_FLG_YES
			])->asArray()
			->all();
	}

    //获取wap首页最新的一条限时抢购记录
    public static function getWapIndexHotSaleList($type,$platform=self::PLATFORM_WAP){
        return self::find()
            ->where([
                'type' 		=>$type,
                'platform'	=>$platform,
                'use_flg'	=>self::USER_FLG_YES
            ])->asArray()->orderBy('create_time desc')
            ->one();
    }
}